﻿var myApp = angular.module('todoApp', []);
// Home Controller
myApp.controller("HomeController", function($scope){
    $scope.task = "";   // The TODO task
    $scope.name = "";   // Task assign to person
    $scope.myTodos = [];// TODO holder - array
    $scope.editMode = false; // app mode - TRUE== is editing, FALSE == is adding
    $scope.currIndex = -1; // current TODO index being EDITED 
    // add a few TODO
    $scope.myTodos.push({ name: "Sally", task: "write 100 lines of code" });
    $scope.myTodos.push({ name: "RickJames", task: "sing loudly" });
    $scope.myTodos.push({ name: "MikeT", task: "super freak" });
    // ADD TODO function
    $scope.doAddTask = function () {
        $scope.myTodos.push({ name: $scope.name, task: $scope.task });
        console.log($scope.task, $scope.name);
    }
    // Remove item - onclick
    $scope.doDeleteTask = function (id) {
        $scope.myTodos.splice(id, 1);
        console.log("THE ID:", id, $scope.myTodos[id]);
    }
    // populate the input fields
    $scope.doEditTask = function (id) {
        console.log("THE ID:", id, $scope.myTodos[id]);
        var todo = $scope.myTodos[id];
        $scope.task = todo.task;
        $scope.name = todo.name;
        $scope.editMode = true;
        $scope.currIndex = id;
    }
    // Save the TODO being edited
    $scope.doSaveTask = function () {
        $scope.myTodos[$scope.currIndex] = { name: $scope.name, task: $scope.task };
        $scope.editMode = false;
        $scope.task = "";
        $scope.name = "";
    }

})
// Temp second controller
myApp.controller("TempController", function ($scope) {
    $scope.greeting = "Hello from temp world!";
})